
#include "queue_pcm.h"




static  audio_data_t  audio_queue;



int queue_audio_pcm_init(void)
{
	int i = 0;

	log_d("audio pcm queue init.\n");

	for(i=0;i<MAX_PCM_SIZE_4K;i++)
	{
		audio_queue.packet[i].data = NULL;
		audio_queue.packet[i].size = 0;
		audio_queue.packet[i].pts  = 0;	
	}
	
	audio_queue.windex    = 0;
	audio_queue.rindex    = 0;

	audio_queue.total     = 0; 
	audio_queue.remain    = 0;

	return 0;
}

int queue_audio_pcm_write(uint8_t *data, int size,int64_t pts)
{
	if(!data){
		log_e("data error\n");
		return -1;
	}
	
	if(!size){
		log_e("size error\n");
		return -1;
	}

	if(audio_queue.packet[audio_queue.windex].data)
	{
		log_d("\n");
		free(audio_queue.packet[audio_queue.windex].data);
		log_d("\n");
		audio_queue.packet[audio_queue.windex].data = NULL;
	}

	audio_queue.packet[audio_queue.windex].data = malloc(size);
	memcpy(audio_queue.packet[audio_queue.windex].data,data,size);
	audio_queue.packet[audio_queue.windex].size = size;
	audio_queue.packet[audio_queue.windex].pts  = pts;

	audio_queue.total = audio_queue.total + size;
	
	audio_queue.windex ++;
	if(audio_queue.windex >= MAX_PCM_SIZE_4K)
	{
		audio_queue.windex = 0;
	}

	return 0;

}


int queue_audio_pcm_read(uint8_t **packet,int* size,int64_t *pts)
{	
	*size   = 0;
	*packet = NULL;
	*pts    = 0;

	if(audio_queue.windex == audio_queue.rindex)
	{
		return -1;
	}

	if(audio_queue.windex <= 0)
	{
		return -1;
	}	
	
	//log_d("w:r=%lld:%lld.\n",audio_queue.windex,audio_queue.rindex);

	*size   = audio_queue.packet[audio_queue.rindex].size;
	*packet = audio_queue.packet[audio_queue.rindex].data;
	*pts    = audio_queue.packet[audio_queue.rindex].pts;

	if(audio_queue.total > audio_queue.packet[audio_queue.rindex].size)
	{
		audio_queue.remain = audio_queue.total - audio_queue.packet[audio_queue.rindex].size;
	}
	else{
		audio_queue.remain = audio_queue.packet[audio_queue.rindex].size;
	}
	
	//log_d("remain=%lld\n",audio_queue.remain);

	audio_queue.rindex++;
	if(audio_queue.rindex >= MAX_PCM_SIZE_4K)
	{
		audio_queue.rindex = 0; 
	}
	
	return 0;
}


int queue_audio_pcm_free(void)
{
	int index = 0;

	if(audio_queue.rindex <= 0)
		return -1;

	index = audio_queue.rindex - 1;

	//log_d("index = %d\n",index);

	if(audio_queue.packet[index].data)
	{
		free(audio_queue.packet[index].data);
		audio_queue.packet[index].data = NULL;
	}
	
	//log_d("\n");

	return 0;
}


int queue_audio_pcm_is_empty(void)
{	
	if(audio_queue.windex == audio_queue.rindex)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


int queue_audio_get_total(int64_t* total)
{
	*total = audio_queue.total;

	return 0;
}

int queue_audio_get_remain(int64_t* remain)
{
	*remain = audio_queue.remain;

	return 0;
}



int queue_audio_pcm_num(void)
{
	if(audio_queue.windex > audio_queue.rindex)
	{
		log_d("audio remain =%d\n",audio_queue.windex - audio_queue.rindex);
	}
	else
	{
		log_d("audio remain =%d\n",audio_queue.rindex - audio_queue.windex);
	}

	log_d("audio remain  size =%d\n",audio_queue.remain);

	return 0;
}




int queue_audio_pcm_deinit(void)
{
	int i = 0;

	log_d("audio pcm queue exit.\n");
	audio_queue.windex = 0;
	audio_queue.rindex = 0;
	
	for(i=0;i<MAX_PCM_SIZE_4K;i++)
	{
		if(audio_queue.packet[i].data)
		{
			free(audio_queue.packet[i].data);
			audio_queue.packet[i].data = NULL;
		}
		
		audio_queue.packet[i].size = 0;	
		audio_queue.packet[i].pts  = 0;
	}
	



	return 0;
}













